package com.itzol.javablog.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.itzol.javablog.config.Result;
import com.itzol.javablog.entity.Users;
import com.itzol.javablog.service.Userservice;
import com.itzol.javablog.util.Jwtutil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private Userservice userservice;
    @PostMapping("/login")
    public Result login(@RequestBody Users user, HttpServletResponse response){
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("loginid",user.getLoginid());
        wrapper.eq("loginpwd",user.getLoginpwd());
        if (userservice.getOne(wrapper)!=null){
            String jwt = Jwtutil.createToken(user.getLoginid());
            response.setHeader("Authorization", jwt);
            response.setHeader("Access-control-Expose-Headers", "Authorization");
            return Result.succ(user.getId());
        }
        else {
            return Result.erreo("账号或密码错误");
        }

    }
     //检查token
    @GetMapping("/checkToken")
    public Boolean checkToken(HttpServletRequest request){
        String token = request.getHeader("token");
        return Jwtutil.checkToken(token);
    }
}
